package com.zt.controller;

import com.zt.entity.Result;
import com.zt.pojo.Order;
import com.zt.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 创建订单
     */
    @PostMapping("/create")
    public Result createOrder(@RequestBody Map<String, Object> params) {
        try {
            Integer userId = params.get("userId") != null ? Integer.valueOf(params.get("userId").toString()) : null;
            Integer courseId = params.get("courseId") != null ? Integer.valueOf(params.get("courseId").toString()) : null;
            Integer teacherId = params.get("teacherId") != null ? Integer.valueOf(params.get("teacherId").toString()) : null;
            Integer couponId = params.get("couponId") != null ? Integer.valueOf(params.get("couponId").toString()) : null;
            Integer bookingId = params.get("appointmentId") != null ? Integer.valueOf(params.get("appointmentId").toString()) : null;
            String courseName = params.get("courseName") != null ? params.get("courseName").toString() : null;

            if (userId == null || courseId == null || teacherId == null) {
                return Result.error("缺少必要参数");
            }


            Order order = ordersService.createOrder(userId, courseId, teacherId, couponId, bookingId, courseName);
            
            return Result.success("订单创建成功", order);
        } catch (NumberFormatException e) {
            return Result.error("参数格式错误：请确保ID为有效的数字");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("创建订单失败：" + e.getMessage());
        }
    }

    /**
     * 更新订单状态
     */
    @PostMapping("/update")
    public Result updateOrder(@RequestBody Map<String, Object> params) {
        try {
            String orderNo = (String) params.get("orderNo");
            String paymentMethod = (String) params.get("paymentMethod");
            String paymentTime = (String) params.get("paymentTime");
            String transactionId = (String) params.get("transactionId");

            if (orderNo == null) {
                return Result.error("订单号不能为空");
            }

            boolean success = ordersService.updateOrderStatus(orderNo, 3); // 3表示已完成
            if (success) {
                return Result.success("订单状态更新成功");
            } else {
                return Result.error("订单状态更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新订单失败：" + e.getMessage());
        }
    }

    /**
     * 获取订单列表
     */
    @GetMapping("/list")
    public Result getOrderList(@RequestParam(required = false) Integer userId) {
        try {
            return Result.success("获取成功", ordersService.getUserOrders(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取订单列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取用户订单列表
     */
    @GetMapping("/user/{userId}")
    public Result getUserOrders(@PathVariable("userId") Integer userId) {
        try {
            List<Order> orders = ordersService.getUserOrders(userId);
            return Result.success("获取成功", orders);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取订单列表失败：" + e.getMessage());
        }
    }
} 

